<template>
	<view class="tui-center">
		<!--<web-view :src="url"></web-view>-->
		<iframe
				id="dramap"
				ref="iframe"
				:src="url"
		/>
	</view>
</template>

<script>
	import { BASE_URL } from '@/env'
	export default {
		name: 'perform',
		components: {

		},
		data() {
			return {
				url: 'https://test.h5.lindingtechnology.com/',
				dataForm: {
                    account: 'super',
                    password: '123456'
				},
                typeId: undefined,
                name: undefined
			}
		},
		onLoad() {
            this.name = getApp().globalData.name;
            if (this.name != undefined) {
                this.Login()
            }
		},
		onShow() {
            /* const url = 'https://test.h5.lindingtechnology.com'
			const timestamp = new Date().getTime()
            // 超时调用，避免程序执行时，webview并未清除，所以给它一个时间间歇
            setTimeout(() => {
                this.url = url + `?timestamp=${timestamp}`;
            },100) */
		},
        async created() {
			const viewUrl = 'https://test.h5.lindingtechnology.com'
			const timestamp = new Date().getTime()
			// 超时调用，避免程序执行时，webview并未清除，所以给它一个时间间歇
			setTimeout(() => {
			    this.url = viewUrl + `?timestamp=${timestamp}`;
			},100)
            await new Promise(resolve => {
                const script = document.createElement('script')
                script.type = 'text/javascript'
                script.src = 'https://res.wx.qq.com/open/js/jweixin-1.2.0.js'
                document.body.appendChild(script)
                script.onload = () => resolve()
            })
            const url = location.href.split('#')[0]
            uni.request({
                url: 'https://test.drawmap.lindingtechnology.com/weapp/getWxConfig',
                data: { url },
                method: 'POST',
                success: (res) => {
                    // console.log(res)
                    wx.config({
                        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                        appId: res.data.data.appid, // 必填，公众号的唯一标识
                        timestamp: res.data.data.timestamp, // 必填，生成签名的时间戳
                        nonceStr: res.data.data.noncestr, // 必填，生成签名的随机串
                        signature: res.data.data.signature, // 必填，签名
                        jsApiList: ['openLocation', 'getLocation'] // 必填，需要使用的JS接口列表
                    })

                    wx.error(function(res) {
                        console.log(JSON.stringify(res) + 'err')
                        // alert(JSON.stringify(res) + 'err')
                    })
                    wx.ready()
                },
                fail: (error => {
                })
            })
			var than = this
            window.onmessage = e => {
                if (e.data.type === 'getLocation') {
                    wx.getLocation({
                        type: 'gcj02',
                        success: (res) => {
                            console.log('getLocation suc' + JSON.stringify(res))
                            const iframeWin = than.$refs.iframe.contentWindow
                            iframeWin.postMessage({ type: 'location', data: res }, '*')
                        },
                        fail: (err) => {
                            console.log('getLocation err' + JSON.stringify(err))
                        }
                    })
                }
                if (e.data.type === 'openLocation') {
                    wx.openLocation({
                        latitude: e.data.data.latitude,
                        longitude: e.data.data.longitude
                    })
                }
            }
        },
		onReady() {

		},
		methods: {
			Login() {
				uni.request({
					url: BASE_URL + 'web/login',
					data: this.dataForm,
                    method: 'POST',
                    success: (res) => {
                        uni.setStorageSync('tokens', res.data.data.token)
                        this.typeList(res.data.data.token)
                    },
                    fail: (error => {
                    })
				})
			},
            typeList(token) {
                console.log(token)
                uni.request({
                    url: BASE_URL + 'type/all',
                    method: 'GET',
                    header: {'client': 'web', 'Authorization': token},
                    success: (res) => {
                        let list = res.data.data
	                    for (let item of list) {
                            if (item.name == this.name) {
                                this.typeId = item.id
                            }
	                    }
                        this.url = `https://test.h5.lindingtechnology.com?typeId=${this.typeId}`
                    },
                    fail: (error => {
                        this.$api.msg( error.message )
                    })
                })
            }
		}
	}
</script>

<style lang="scss">
	.tui-center {
		width: 100%;
		height: 95vh;
		padding-bottom: 30rpx;
	}
	#dramap{
		width: 100%;
		height: 95vh;
		border: 0;
	}
</style>
